package com.samoilovich.belarustoday.handlers;

import android.util.Log;

import com.samoilovich.belarustoday.datemodel.Rates;
import com.samoilovich.belarustoday.datemodel.RatesModel;
import com.samoilovich.belarustoday.rates.RatesActivity;
import com.samoilovich.belarustoday.tools.Utility;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;

public class RatesHandler {
    private final String TAG = getClass().getName();
    private final String RATES_URL = "http://belarustoday.by/belarus/api/exchange.php";

    private final String EXCHANGE_TIME = "exchange_time";
    private final String EXCHANGE_ARRAY = "exchange_array";
    private final String BANK = "bank";
    private final String BUY = "buy";
    private final String SELL = "sell";

    private final String DOLLAR = "dollar";
    private final String EURO = "euro";
    private final String RUB = "rub";
    private final String EURO_DOLLAR = "euroDollar";
    private final String DOLLAR_EURO = "dollarEuro";

    private static RatesHandler sInstance;
    private Rates mRates;

    public static RatesHandler getInstance() {
        if (sInstance == null) {
            sInstance = new RatesHandler();
        }

        return sInstance;
    }

    private RatesHandler() {
        mRates = new Rates();
    }

    public void getRates(final RatesActivity listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpResponse response;
                HttpClient client;
                HttpParams params;

                try {
                    params = Utility.createHttpParams();
                    client = new DefaultHttpClient(params);
                    response = client.execute(new HttpGet(RATES_URL));
                    InputStream stream = response.getEntity().getContent();
                    String json = Utility.convertStreamToString(stream);
                    parseCategoriesJSON(json);
                    listener.update(mRates);
                    Log.d(TAG, json);
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        }).start();
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    private void parseCategoriesJSON(String json) {
        JSONArray temp;

        try {
            mRates = new Rates();
            JSONObject obj = new JSONObject(json);
            mRates.setTime(obj.getString(EXCHANGE_TIME));
            JSONArray array = obj.getJSONArray(EXCHANGE_ARRAY);
            for (int i = 0; i < array.length(); i++) {
                temp = array.getJSONArray(i);
                switch (i) {
                    case 0:
                        ArrayList<RatesModel> dollars = parseRatesInfo(temp);
                        mRates.setDollars(dollars);
                        break;

                    case 1:
                        ArrayList<RatesModel> euros = parseRatesInfo(temp);
                        mRates.setEuros(euros);
                        break;

                    case 2:
                        ArrayList<RatesModel> rubs = parseRatesInfo(temp);
                        mRates.setRubs(rubs);
                        break;

                    case 3:
                        ArrayList<RatesModel> euroDollars = parseRatesInfo(temp);
                        mRates.setEurosDollars(euroDollars);
                        break;

                    case 4:
                        ArrayList<RatesModel> dollarsEuros = parseRatesInfo(temp);
                        mRates.setDollarsRubs(dollarsEuros);
                        break;
                }
            }

        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }

    // -----------------------------------------------------------------------------------
    private ArrayList<RatesModel> parseRatesInfo(JSONArray array) {
        JSONObject temp;
        RatesModel model;
        ArrayList<RatesModel> container = new ArrayList<RatesModel>();

        try {
            for (int i = 0; i < array.length(); i++) {
                model = new RatesModel();
                temp = array.getJSONObject(i);
                model.setBank(temp.getString(BANK));
                model.setBuying(temp.getString(BUY));
                model.setSale(temp.getString(SELL));
                container.add(model);
            }
        } catch (Exception exp) {
            exp.printStackTrace();
        }

        return  container;
    }
}
